package com.zlzxm.studentscreen.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;

/**
 * CREATE BY zlzxm ON 11/25/20
 */
public class FileUtil {

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static File createFile(String path){

        File file = new File(path);

        if(file.exists()){

            return file;
        }else {

            File parent = file.getParentFile();

            if(parent != null){

                if(!parent.exists() ){

                    if( !parent.mkdirs()) {

                        return null;
                    }

                }

                try {

                    file.createNewFile();

                    return  file;
                } catch (IOException e) {
                    e.printStackTrace();

                    return  null;
                }


            }else {

                //没有这种情况

                return  null;

            }

        }

    }


    public static void reCreateFile(String path){


        if(FileUtil.isFileExists(path)){

            File file = new File(path);

            //noinspection ResultOfMethodCallIgnored
            file.delete();

        }

        createFile(path);

    }

    public static boolean isFilePathUseAble(String path){

        if(TextUtils.isEmpty(path)){

            return false;
        }


        return isFileExists(path);

    }


    public static boolean isFileExists(String path){

        if(TextUtils.isEmpty(path)){

            return false;
        }

        File file = new File(path);

        if(file == null){

            return false;
        }

        return  file.exists();

    }


    public  static String  getFileName(String url){

        int start = url.lastIndexOf("/")+1;

        return  url.substring(start);

    }

    public  static String  getFileNameWithoutExe(String url){

        int start = url.lastIndexOf("/")+1;
        int end =0;
        if(url.contains(".")) {
            end = url.lastIndexOf(".");
        }else {

            end = url.length();
        }

        return  url.substring(start,end);

    }


    public static void deleteFile(String path){

        if(isFilePathUseAble(path)){

            File file = new File(path);

            if(file.isDirectory()){

                File[] files = file.listFiles();

                for (File subFile : files) {

                    if(subFile.isDirectory()){

                        deleteFile(subFile.getPath());

                    }else {

                        subFile.delete();

                    }

                }

            }
            file.delete();


        }

        return;
    }

    public static void sysToScan(Context context, String path){

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        File file = new File(path);

        Uri uri =Uri.fromFile(file);

        intent.setData(uri);

        context.sendBroadcast(intent);



    }

}
